#include "Cursor.h"
#include <GL/gl.h>
#include <SDL/SDL.h>
#include <iostream>
using namespace std;

Cursor::Cursor(CubePile * (*aCubePiles)[GRID_SIZE])
{
	cubePiles = aCubePiles;
	xPos = 0;
	yPos = 0;
	zPos = 0;
	xInd = 0;
	zInd = 0;
	heldCube = NULL;
}

Cursor::~Cursor()
{
	
}

void Cursor::render()
{
	calculatePosition();
	
	glEnable(GL_BLEND);
	glTranslatef(xPos, yPos, zPos);
	
	glBegin(GL_QUADS);
	
	glColor4f(1.0f,1.0f,0.0f, 0.6f);			// Full Brightness, 50% Alpha ( NEW )
	
	
	//bottom face
	glVertex3f( 1.0f,-0.5f, 1.0f);			// Top Right Of The Quad (Bottom)
	glVertex3f(-1.0f,-0.5f, 1.0f);			// Top Left Of The Quad (Bottom)
	glVertex3f(-1.0f,-0.5f,-1.0f);			// Bottom Left Of The Quad (Bottom)
	glVertex3f( 1.0f,-0.5f,-1.0f);			// Bottom Right Of The Quad (Bottom)
	
	if(heldCube != NULL)
	{
		
		//TOP
		glVertex3f( 1.0f, 0.5f,-1.0f);			// Top Right Of The Quad (Top)
		glVertex3f(-1.0f, 0.5f,-1.0f);			// Top Left Of The Quad (Top)
		glVertex3f(-1.0f, 0.5f, 1.0f);			// Bottom Left Of The Quad (Top)
		glVertex3f( 1.0f, 0.5f, 1.0f);			// Bottom Right Of The Quad (Top)
	
		//front face
		glVertex3f(-1, 0.5, 1);
		glVertex3f( 1, 0.5, 1);
		glVertex3f( 1,-0.5, 1);
		glVertex3f(-1,-0.5, 1);
		
		//back face
		glVertex3f( 1.0f,-0.5f,-1.0f);			// Bottom Left Of The Quad (Back)
		glVertex3f(-1.0f,-0.5f,-1.0f);			// Bottom Right Of The Quad (Back)
		glVertex3f(-1.0f, 0.5f,-1.0f);			// Top Right Of The Quad (Back)
		glVertex3f( 1.0f, 0.5f,-1.0f);			// Top Left Of The Quad (Back)
		
		//right face
		glVertex3f( 1.0f, 0.5f,-1.0f);			// Top Right Of The Quad (Right)
		glVertex3f( 1.0f, 0.5f, 1.0f);			// Top Left Of The Quad (Right)
		glVertex3f( 1.0f,-0.5f, 1.0f);			// Bottom Left Of The Quad (Right)
		glVertex3f( 1.0f,-0.5f,-1.0f);			// Bottom Right Of The Quad (Right)
		
		//left face
		glVertex3f(-1.0f, 0.5f, 1.0f);			// Top Right Of The Quad (Left)
		glVertex3f(-1.0f, 0.5f,-1.0f);			// Top Left Of The Quad (Left)
		glVertex3f(-1.0f,-0.5f,-1.0f);			// Bottom Left Of The Quad (Left)
		glVertex3f(-1.0f,-0.5f, 1.0f);			// Bottom Right Of The Quad (Left)
	}	
	glEnd();
	glTranslatef(-xPos, -yPos, -zPos);
	glDisable(GL_BLEND);
}

void Cursor::calculatePosition()
{
	checkBounds();
	Cube * topCube = getCurrentPile()->peekAtTop();
	xPos = topCube->xPos;
	zPos = topCube->zPos;
	yPos = topCube->yPos + CUBE_HEIGHT + CUBE_GAP;
}

CubePile * Cursor::getCurrentPile()
{
	return cubePiles[xInd][zInd];
}

void Cursor::grabOrDrop()
{
	if(heldCube == NULL)
	{
		grab();
	}
	else
	{
		drop();
	}
}

void Cursor::grab()
{
	heldCube = getCurrentPile()->popCube();
}

void Cursor::drop()
{
	if(getCurrentPile()->size() < MAX_CUBES_IN_PILE)
	{
		Cube * topCube = getCurrentPile()->peekAtTop();
		heldCube->xPos = topCube->xPos;
		heldCube->zPos = topCube->zPos;
		heldCube->yPos = CUBE_Y_DROP;
		getCurrentPile()->dropCube(heldCube);
		heldCube = NULL;
	}
}

void Cursor::checkBounds()
{
	if(xInd < 0)
	{
		xInd = 0;
	}
	if (xInd >= GRID_SIZE)
	{
		xInd = GRID_SIZE -1;
	}
	if (zInd < 0)
	{
		zInd = 0;
	}
	if(zInd >= GRID_SIZE)
	{
		zInd = GRID_SIZE -1;
	}
}

void Cursor::handleInput(bool keys[321], float timeD)
{
	//TODO: changing key state is really bad practice, 
	//      it would be nice to find a better way around this
	if(keys[SDLK_UP])
	{
		zInd--;
		keys[SDLK_UP] = false;
	}
	if(keys[SDLK_DOWN])
	{
		zInd++;
		keys[SDLK_DOWN] = false;
	}
	if(keys[SDLK_RIGHT])
	{
		xInd++;
		keys[SDLK_RIGHT] = false;
	}
	if(keys[SDLK_LEFT])
	{
		xInd--;
		keys[SDLK_LEFT] = false;
	}
	if(keys[SDLK_SPACE])
	{
		grabOrDrop();
		keys[SDLK_SPACE] = false;
	}
}
